package cn.suimg.neo4j.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 */
public class DateUtil {

    /**
     * 一秒
     */
    public static final int SECOND = 1000;

    /**
     * 一分钟
     */
    public static final int MINUTE = SECOND * 60;

    /**
     * 一小时
     */
    public static final int HOUR = MINUTE * 60;

    /**
     * 一天
     */
    public static final int DAYS = HOUR * 24;

    /**
     * 精确到当前日期
     */
    private static final SimpleDateFormat yearFormatter = new SimpleDateFormat("yyyy");

    /**
     * 精确到当前日期
     */
    private static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 精确到时分秒
     */
    private static final SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm.ss");

    /**
     * 无效日期
     */
    public static final Date NULL_DATE = new Date(0L);

    /**
     * 当前星期几文案列表
     */
    private static final String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    /**
     * 获取当前日期
     *
     * @return
     */
    public static String getCurrentDate() {
        synchronized (DateUtil.class) {
            return dateFormatter.format(new Date());
        }
    }

    /**
     * 只获取当前年份
     *
     * @return
     */
    public static String getCurrentYear() {
        synchronized (DateUtil.class) {
            return yearFormatter.format(new Date());
        }
    }

    /**
     * 获取当前的时分秒
     *
     * @return
     */
    public static String getCurrentDateTime() {
        synchronized (DateUtil.class) {
            return dateTimeFormatter.format(new Date());
        }
    }

    /**
     * 格式化时间
     * @param date
     * @return
     */
    public static String parseDateTime(Date date){
        return dateTimeFormatter.format(date);
    }

    /**
     * 取得指定日期当天的开始时间 00:00:00
     * 参数 date
     * 返回
     */
    public static Date getBegTimeOfDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date start = calendar.getTime();
        return start;
    }

    /**
     * 取得指定日期当天的开始时间 23:59:59
     * 参数 date
     * 返回
     */
    public static Date getEndTimeOfDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.SECOND, -1);
        calendar.set(Calendar.MILLISECOND, 0);
        Date end = calendar.getTime();
        return end;
    }

    /**
     * 获取日期是星期几
     *
     * @param date
     * @return
     */
    public static String getWeekOfDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayOfWeek < 0) {
            dayOfWeek = 0;
        }
        return weekDays[dayOfWeek];
    }

    /**
     * 解析秒为几分几秒
     * @param theTime
     * @return
     */
    public static String parseSecond(int theTime){
        int middle = 0;
        int hour = 0;
        if(theTime > 60) {
            middle= theTime / 60;
            theTime = theTime % 60;
            if(middle> 60) {
                hour= middle / 60;
                middle= middle % 60;
            }
        }
        String result = "" + theTime + "秒";
        if(middle > 0) {
            result = "" + middle + "分" + result;
        }
        if(hour> 0) {
            result = "" + hour + "小时" + result;
        }
        return result;
    }

    /**
     * 获取当前日期是星期几
     *
     * @return
     */
    public static String getWeekOfDate() {
        return getWeekOfDate(new Date());
    }
}
